<?php
namespace app\modules\developer\models;

use Yii;
use yii\base\Model;

class MenuSearch extends Menu
{

    public function rules()
    {
        return [
            ['id', 'safe'],
            ['parentid', 'integer', 'message' => 'parentid必须为数字，或数字字符串', 
                         'min'=>0, 'tooSmall' => 'parentid最小存储0', 
                         'max'=>65535, 'tooBig' => 'parentid最大存储65535'],
            ['showflag', 'integer', 'message' => 'showflag必须为数字，或数字字符串'],
            ['showflag', 'in', 'range' => array(0, 1, 2, 3, 4), 'message' => 'showflag值必须在0,1,2,3,4中选择'],
            ['display', 'default', 'value' => 2],
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(0, 1, 2), 'message' => 'display值必须在0,1,2中选择'],
            //如果是1的话，是取当前应用的所有菜单，包括其他菜单的顶级菜单；（登录后获取菜单）
            //如果是2的话，是取当前应用的所有菜单，包括认证系统的所有菜单；主要为了权限设置 如果comid=00000000需要展示 开发者，否则不需要(权限那块用到)
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($params)
    {
        $data['MenuSearch'] = $params;
        // P($data);

        $query = Menu::find();

        if (!($this->load($data) && $this->validate())) {
            return false;
        }
        // P($this);

        if(isset($this->id) and count($this->id) >= 0){
            // P($this->id);
            $query -> andWhere([
                'in', 'id', $this->id
            ]); 
        }

        if(isset($this->parentid)){
            $query -> andWhere([
                'parentid' => $this->parentid,
            ]);
        }

        if(isset($this->showflag)){
            if($this->showflag == 0){//除了内页显示的都取出来
                $query -> andWhere(['or',
                    ['showflag' => 1],
                    ['showflag' => 2],
                    ['showflag' => 3],
                    ['showflag' => 4]
                ]);
            }else if($this->showflag == 2){
                $query -> andWhere(['or',
                    ['showflag' => 1],
                    ['showflag' => 2],
                    ['showflag' => 4]
                ]);
            }else if($this->showflag == 3){
                $query -> andWhere(['or',
                    ['showflag' => 1],
                    ['showflag' => 3],
                    ['showflag' => 4]
                ]);
            }else{
                $query -> andWhere([
                    'showflag' => $this->showflag,
                ]);            
            }
        }

        // P($this->display);
        if(isset($this->display)){
            if($this->display == 0){
                $query -> andWhere(['or',
                    ['{{%menu}}.display' => 1],
                    ['{{%menu}}.display' => 2]
                ]);
            }else{
                $query -> andWhere([
                    '{{%menu}}.display' => $this->display,
                ]);            
            }
        }
        // P($query);

        return $query;
    }

    
}